package cn.edu.tju.fctl.rest;

import cn.edu.tju.fctl.entities.LeaveInfo;
import cn.edu.tju.fctl.entities.TimeList;
import cn.edu.tju.fctl.entities.VacationView;
import cn.edu.tju.fctl.service.IVacationService;
import cn.edu.tju.fctl.util.BizCode;
import cn.edu.tju.fctl.util.ShiroUtil;
import cn.edu.tju.fctl.util.StateCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Generated;
import javax.validation.constraints.AssertFalse;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import java.sql.Time;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Stack;

/**
 * Created by liutong on 15/12/25.
 */
@Component
@Path("vacation_view")
public class VacationViewRest {
    @Autowired
    IVacationService vacationService;

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public Object getVacationView(@QueryParam("page") final int page,
                                  @QueryParam("num") final int num,
                                  @QueryParam("starttime") final String starttime_str,
                                  @QueryParam("endtime") final String endtime_str){
        Date starttime = new Date();
        starttime.setTime(Long.parseLong(starttime_str));
        Date endtime = new Date();
        endtime.setTime(Long.parseLong(endtime_str));

        List<VacationView> resultSet = vacationService.getVacationView();
        for (VacationView vacationView : resultSet){
            List<TimeList> fileredLeaveTimes = new ArrayList<TimeList>();
            List<TimeList> fileredDelayTimes = new ArrayList<TimeList>();
            for (TimeList leaveTimeItem : vacationView.getLeaveTimes()){
                TimeList temp = new TimeList();
                Date temp_starttime = leaveTimeItem.getStarttime().after(starttime) ? leaveTimeItem.getStarttime() : starttime;
                Date temp_endtime = leaveTimeItem.getEndtime().before(endtime) ? leaveTimeItem.getEndtime() : endtime;
                if(leaveTimeItem.getStarttime().before(starttime) && leaveTimeItem.getEndtime().after(endtime)){
                    temp_starttime = leaveTimeItem.getStarttime();
                    temp_endtime = leaveTimeItem.getEndtime();
                }
                if(temp_starttime.before(temp_endtime)){
                    temp.setStarttime(temp_starttime);
                    temp.setEndtime(temp_endtime);
                    fileredLeaveTimes.add(temp);
                }
            }
            for (TimeList leaveDelayTimeItem : vacationView.getLeaveDelayTimes()){
                TimeList temp = new TimeList();
                Date temp_starttime = leaveDelayTimeItem.getStarttime().after(starttime) ? leaveDelayTimeItem.getStarttime() : starttime;
                Date temp_endtime = leaveDelayTimeItem.getEndtime().before(endtime) ? leaveDelayTimeItem.getEndtime() : endtime;
                if(leaveDelayTimeItem.getStarttime().before(starttime) && leaveDelayTimeItem.getEndtime().after(endtime)){
                    temp_starttime = leaveDelayTimeItem.getStarttime();
                    temp_endtime = leaveDelayTimeItem.getEndtime();
                }
                if (temp_starttime.before(temp_endtime)){
                    temp.setStarttime(temp_starttime);
                    temp.setEndtime(temp_endtime);
                    fileredDelayTimes.add(temp);
                }
            }
            vacationView.setLeaveTimes(fileredLeaveTimes);
            vacationView.setLeaveDelayTimes(fileredDelayTimes);
        }

        return resultSet;
    }
}
